package com.sport.facilities.booking.system.httptask;
import android.content.Context;
import android.util.Log;

import com.sport.facilities.booking.system.htmlchecker.LoginResultChecker;
import com.sport.facilities.booking.system.model.UserInfo;

public class LoginResultTask extends BasicTask {
	private final String TAG = "LoginResultTask";
	
	public LoginResultTask(Context ctx,UserInfo uInfo,OnTaskCompleted completeAction) {
		super(ctx,uInfo);
		this.listener = completeAction;
	}

	/*
	 * Special Logic only for PolyU Connection Here:
	 */
	public void fnLogin() {
		try {
			if (uInfo != null) {
				super.onPreExecute();
				super.execute(PATH_LOGIN);
			} else {
				throw new Exception(
						"Cannot find user information, please try again.");
			}
		} catch (Exception e) {
			this.listener.onTaskFailed(e.getMessage());
		}
	}
	
	public void fnLoadLoginPage() {
		super.execute(PATH_LOGIN_PAGE);
	}
	
	@Override
    protected void onPostExecute(String result) {
		super.onPostExecute(result);
		Log.v(TAG, "onPostExecute");
		try{
			LoginResultChecker lcr = new LoginResultChecker();
			lcr.hasLogin(result);
			this.listener.onTaskCompleted("Login success");
		}catch(Exception e){
			this.listener.onTaskFailed(e.getMessage());
		}
    }

	public void setCompletedAction(OnTaskCompleted completeAction) {
		this.listener = completeAction;
	}

}
